package com.smart.home;

import cn.hutool.core.io.FileUtil;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.UnsupportedEncodingException;

@SpringBootApplication
public class SmartHomeApplication {

    //static String path = "D:\\project\\yisi_ssl\\Project1\\x64\\Debug\\Project1.dll";
    static String path = "/yisi/ssl_cplusplus/libyisissl.so";

    //static String publicPath = "D:\\openssl\\OpenSSL-Win64\\bin\\public_key.pem";
    static String publicPath = "/yisi/ssl_cplusplus/rsa_public_key.pem";

    //static String privatePath = "D:\\openssl\\OpenSSL-Win64\\bin\\private_key.pem";
    static String privatePath = "/yisi/ssl_cplusplus/rsa_private_key.pem";

    public static void main(String[] args) {
        SpringApplication.run(SmartHomeApplication.class, args);
    }

    private static byte[] wrapByte(byte[] bytes){

        int len = bytes.length;

        byte[] arr = new byte[len + 5 ];

        byte[] length = {(byte) (len), (byte) (len >> 8), (byte) (len >> 16), (byte) (len >> 24)};

        for (int i = 0; i < arr.length-1; i++) {
            if (i < 4) {
                arr[i] = length[i];
            } else {
                arr[i] = bytes[i - 4];
            }
        }
        arr[len+4] = '\0';
        return arr;
    }


    public static void test(){
        System.load(path);

        File publicFile = new File(publicPath);
        byte[] pubs = FileUtil.readBytes(publicFile);
        byte[] wrapPub = wrapByte(pubs);

        File privateFile = new File(privatePath);
        byte[] pris = FileUtil.readBytes(privateFile);
        byte[] wrapPri = wrapByte(pris);


        String message = "我是中国人，我哎w是我的祖国";

        JniApi jniApi = new JniApi();

        byte[] bytes = message.getBytes();

        byte[] arr = wrapByte(bytes);


        byte[] ss = jniApi.encrypt(arr, wrapPub);

        System.out.println("加密后为：" +new String(ss));

        byte[] array = jniApi.decrypt(ss, wrapPri);

        int si = (array[0] & 0xff) |((array[1] & 0xff) << 8 )|((array[2] & 0xff) << 16) | ((array[3] & 0xff) << 24);

        byte[] tee = new byte[si];

        System.arraycopy(array,4,tee,0,si);

        System.out.println("解密后为:" + new String(tee));

        byte[] sign = jniApi.sign(arr, wrapPri);

        System.out.println("签名后为：" + new String(sign));

        //int aa = (sign[0] & 0xff) |((sign[1] & 0xff) << 8 )|((sign[2] & 0xff) << 16) | ((sign[3] & 0xff) << 24);

       // byte[] tw = new byte[aa];

        //System.arraycopy(sign,4,tw,0,aa);

        byte[] res = jniApi.verify(arr, sign, wrapPub);

        int se = (res[0] & 0xff) |((res[1] & 0xff) << 8 )|((res[2] & 0xff) << 16) | ((res[3] & 0xff) << 24);

        byte[] pp = new byte[se];

        System.arraycopy(res,4,pp,0,se);

        System.out.println("验签：" + (int)pp[0]);

    }
}
